//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios


//自己写前置资源拦截器
//=======================前端登录相关的拦截器配置========
//1.axios前置拦截器 写到token到后端
axios.interceptors.request.use(config=>{
    //携带token
    let token =  localStorage.getItem("token");//514dc79b-2b9f-48be-88a8-1b012dee80ab
    if(token){
        config.headers['token']=token;//514dc79b-2b9f-48be-88a8-1b012dee80ab
    }
    return config;//一定要返回
},error => {
    Promise.reject(error);
});

//3.axios后置拦截器
axios.interceptors.response.use(result=>{
    console.log(result.data+"jjjjjjj");
    let data = result.data;//{"success":false,"message":"noLogin"}
    if(!data.success && data.message==="noLogin"){//只处理后端登录拦截器报错的信息
        //清除以前的过期登录信息
        localStorage.removeItem("token");
        localStorage.removeItem("loginInfo");
        //next({ path: '/login' });
        //2.  跳转到登录页
        location.href="login.html"
    }
    return result;
},error => {
    Promise.reject(error);
});

//4.前端静态资源拦截器
let url = location.href;
if(url.indexOf("register.html")===-1
    && url.indexOf("login.html")===-1
    && url.indexOf("index.html")===-1
    && url.indexOf("callback.html")===-1
    && url.indexOf("binder.html")===-1){
    let loginInfo = localStorage.getItem("loginInfo");
    if(!loginInfo){
        //清除以前的过期登录信息
        localStorage.removeItem("token");
        localStorage.removeItem("loginInfo");
        location.href="login.html";
    }
}

//=======================前端登录相关的拦截器配置========

/**
 * 解析url获取参数对象
 * @param url
 * @returns {{}}
 */
function parseUrlParams2Obj(url){
    //获取请求参数
    let paramStr = url.substring(url.indexOf("?")+1);//code=021FdXFa14mYlA016OGa1L3R4X0FdXFO&state=1
    let paramStrs = paramStr.split("&");//[code=021FdXFa14mYlA016OGa1L3R4X0FdXFO,state=1]
    let paramObj = {};
    for(let i=0;i<paramStrs.length;i++){
        let paramName = paramStrs[i].split("=")[0];//code
        let paramValue = paramStrs[i].split("=")[1];//021FdXFa14mYlA016OGa1L3R4X0FdXFO
        paramObj[paramName] = paramValue;
    }
    return paramObj;
}